from lxml import etree
from StringIO import StringIO
import os, sys

MYPATH = "C:\\Program Files\\OpenLaszlo Server 4.0.3\\Server\\lps-4.0.3\\lps\\components\\"

def walkLibraries(rootpath):
    list = []
    for filename in listFilesInDirectories(rootpath):
        if filename[-4:] == ".lzx":
            for a in getClasses(filename):
                list.append( a )
    list.sort()
    print list

def listFilesInDirectories(startdirectory):
    stack = [startdirectory]
    files = []
    while stack:
        directory = stack.pop()
        for file in os.listdir(directory):
            fullname = os.path.join(directory, file)
            files.append(fullname)
            if os.path.isdir(fullname) and not os.path.islink(fullname):
                stack.append(fullname)
    return files

def getClasses(filename):
    list = []
    try:
        tree = etree.parse(filename)
#        print "-"*80
#        print filename
        for foundClass in tree.xpath("/library/class"):
            #print " -"+"-"*20
            list.append( foundClass.get('name') )
#            print     "  C-- "+foundClass.get('name').upper()
#            for attrs in foundClass.keys():
#                print "   P-- "+str(attrs) + " ("+ foundClass.get(attrs)+")"
#
#            for att in foundClass.xpath('attribute'):
#                print "    A-- "+str( att.get('name') ) 
#                for attrs in att.keys():
#                    print "       P-- "+str(attrs) + " ("+ att.get(attrs)+")"
#            for meth in foundClass.xpath('method'):
#                print "    M-- "+str( meth.get('name') ) 
#                for attrs in meth.keys():
#                    print "       P-- "+str(attrs) + " ("+ meth.get(attrs)+")"
    except:
        print "error reading: "+filename
        return []
    return list


walkLibraries(MYPATH)